import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_deer/database/beans/get_user_info_entity.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/util/device_info_plus.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/util/restart_util.dart';
import 'package:flutter_deer/widgets/base_dialog.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';

class SettingPage1Person extends StatefulWidget {
  const SettingPage1Person({super.key});

  @override
  State<SettingPage1Person> createState() => _SettingPage1PersonState();
}

class _SettingPage1PersonState extends State<SettingPage1Person> {
  String name = '';
  String phone = '';
  RxString windowsInfo = ''.obs;

  @override
  void initState() {
    super.initState();
    name = GetUserInfoEntity.get()?.name ?? '';
    phone = GetUserInfoEntity.get()?.username ?? '';
    // 获取应用版本号
    // version = await PackageInfo.fromPlatform().then((value) => value.version);
    // 获取窗口信息
    DeviceUniqueIdUtil.getUniqueId().then((value) {
      print('value: ${value?.toMap()}');
      windowsInfo.value = '''系统: ${value?.productName} (${value?.editionId} ${value?.displayVersion})
时间: ${value?.installDate}
名称: ${value?.computerName} (${value?.userName})
SN\t: ${value?.deviceId.toMd5}''';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 头像
            const CircleAvatar(radius: 50, backgroundColor: Colours.bg, child: Icon(Icons.account_circle, size: 80, color: Colors.white)),
            const SizedBox(height: 20),

            // 用户名文本
            Text(name, style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black87)),
            const SizedBox(height: 8),

            // 手机号文本
            Text(phone, style: const TextStyle(fontSize: 16, color: Colors.grey)),
            const SizedBox(height: 30),

            // 按钮
            SizedBox(
              width: 200,
              height: 45,
              child: ElevatedButton(
                onPressed: () {
                  showDialog<void>(context: context, builder: (_) => const ExitDialog());
                },
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colours.app_main,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30),
                  ),
                ),
                child: const Text('重新登录', style: TextStyle(fontSize: 16, color: Colors.white)),
              ),
            ),
            const SizedBox(height: 40),

            // 版本号 - 添加点击复制功能
            Obx(() => GestureDetector(
                  onTap: () {
                    if (windowsInfo.value.isNotEmpty) {
                      _copyToClipboard(windowsInfo.value);
                    }
                  },
                  child: Text(
                    windowsInfo.value,
                    style: const TextStyle(
                      fontSize: 14,
                      color: Colors.grey,
                    ),
                  ),
                )),
          ],
        ),
      ),
    );
  }

  // 添加复制功能的方法
  void _copyToClipboard(String text) {
    Clipboard.setData(ClipboardData(text: text));
    // 可以添加一个提示，告知用户已复制
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('已复制到剪贴板')),
    );
  }
}

class ExitDialog extends StatefulWidget {

  const ExitDialog({
    super.key,
  });

  @override
  _ExitDialog createState() => _ExitDialog();

}

class _ExitDialog extends State<ExitDialog> {

  @override
  Widget build(BuildContext context) {
    return BaseDialog(
      title: '提示',
      child: const Padding(
        padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
        child: Text('您确定要退出登录吗？', style: TextStyles.textSize16),
      ),
      onPressed: () {
        // 重启应用
        // Restart.restartApp();
        RestartUtil.restartApp();
      },
    );
  }
}